import csv
from datetime import datetime

import matplotlib.pyplot as plt

plt.style.use('seaborn')
plt.rcParams['font.sans-serif']=['SimHei']#设置中文字体

filename="D:\GITEE\study_code\sitka_weather_2018_simple.csv"
with open(filename) as f:
    reader=csv.reader(f)
    header_row=next(reader)
    i,hT,lT,d,n=0,0,0,0,0
    for name in header_row:
        if name=='TMAX':
            hT=i
            i+=1
        elif name=='TMIN':
            lT=i
            i+=1
        elif name=='STATION':
            n=i
            i+=1
        elif name=='DATE':
            d=i
            i+=1
        else:
            i+=1

    #从文件中获取日期，最高温度和最低温度
    dates,highs,lows=[],[],[]
    for row in reader:
        current_date=datetime.strptime(row[d],'%Y-%m-%d')
        high=int(row[hT])
        low=int(row[lT])
        dates.append(current_date)
        highs.append(high)
        lows.append(low)
        station_name=row[n]

#根据最高温度和最低温度绘制图形
fig,ax=plt.subplots()
ax.plot(dates,highs,c='red',alpha=0.5)
ax.plot(dates,lows,c='blue',alpha=0.5)
ax.fill_between(dates,highs,lows,facecolor='blue',alpha=0.1)

#设置图形的格式
title=f'2018年每日最高和最低温度\n{station_name}'
ax.set_title(title,fontsize=20)
ax.set_xlabel('',fontsize=16)
fig.autofmt_xdate()
ax.set_ylabel('温度(F)',fontsize=16)
plt.ylim(0,120)
ax.tick_params(axis='both',which='major',labelsize=16)


plt.show()